"ui";


停止其余脚本()
//files.remove("./kuaishouhongbao.js")
var time = new Date().getTime();
var moday = new java.text.SimpleDateFormat("yyyyMMdd").format(new Date(time))
files.createWithDirs("/sdcard/" + moday.toString() + "快手极速版红包时间记录文件.txt")
var numsto = storages.create(moday.toString() + "cun")
var sto = storages.create("测试次数计数")
var time = new Date().getTime();
var moday = new java.text.SimpleDateFormat("yyyyMMdd").format(new Date(time))
wx = false
ui.layout(
    <ScrollView>
        <vertical>
            <appbar>
                <toolbar title="快手红包助手"/>
            </appbar>
            
            <Switch id="autoService" gravity="left" text="无障碍服务：" checked="{{auto.service != null}}" padding="10 15 220 10" textSize="18sp"/>
            
            
            <horizontal id="jhyc" margin="10 10">
                <text text="" textColor="#f2be45" textSize="13sp" marginLeft="5"gravity="center"/>
                <text  id="jihuoma"text="" textColor="#f2be45" textSize="13sp" margin="0"gravity="center"/>
                
                
                
            </horizontal>
            
            <horizontal id="dqyc"margin="10 0 10 10">
                <text text="" textColor="#f2be45" textSize="13sp" marginLeft="5"gravity="center"/>
                <text  id="daoqishijian" text="" textColor="#f2be45" textSize="13sp" margin="0"gravity="center"/>
                
                
                
            </horizontal>
            <horizontal w="*" h="1sp" margin="0 10 0 10" bg='#E2E2E2'>
            </horizontal>
            
            <horizontal margin="10 0 10 10">
                <text text="本日红包上限：" textColor="black" textSize="15sp" gravity="center" margin="1"/>
                
                
                <input  id="ms1" gravity="center"inputType="number" w="*"  hint="必填" margin="1" />
                
            </horizontal>
            <horizontal margin="10 0 10 10">
                <text text="随机关注概率：" textColor="black" textSize="15sp" gravity="center" margin="1"/>
                
                
                <input  id="ms2" gravity="center"inputType="number" w="*"   margin="1" />
                
            </horizontal>
            
            <horizontal id="yincang"margin="10 0 10 10">
                <text text="随机点赞概率：" textColor="black" textSize="15sp" gravity="center" margin="1"/>
                
                
                <input  id="ms3"  gravity="center"inputType="number" w="*"   margin="1" />
                
            </horizontal>
            <horizontal w="*" h="1sp" margin="0 10 0 10" bg='#E2E2E2'>
            </horizontal>
            <horizontal margin="10 0 10 10">
                <text text="随机观看时间(秒)：" textColor="black" textSize="15sp" gravity="center" margin="1"/>
                
                
                <input  id="ms4"  w="80"  inputType="number" gravity="center"margin="1" />
                
                <text text="——" textColor="black" textSize="15sp" gravity="center" margin="1"/>
                <input  id="ms7"  w="80"  gravity="center"inputType="number" margin="1" />
                
                
            </horizontal>
            <horizontal  margin="10 0 10 10">
                <text text="每开" textColor="black" textSize="15sp" gravity="center" margin="1"/>
                
                <input  id="ms5"  w="80"  gravity="center"inputType="number" margin="1" />
                <text text="个红包休息" textColor="black" textSize="15sp" gravity="center" margin="1"/>
                <input  id="ms6"  w="80"  gravity="center"inputType="number" margin="1" />
                <text text="秒" textColor="black" textSize="15sp" gravity="center" margin="1"/>
                
            </horizontal>
            
            <horizontal w="*" h="1sp" margin="0 10 0 20" bg='#E2E2E2'>
            </horizontal>
            <text text="注：按音量上键停止脚本，请手动打开无障碍服务和后台弹出界面权限(华为需要在安全中心设置关联启动)，小米部分机型需要设置获取剪切板权限" textColor="black" textSize="16sp" gravity="left" margin="10"/>
            
            <horizontal gravity="center">
                
                <button id="start1" w="200"style="Widget.AppCompat.Button.Colored" margin="20 20"text="启动"/>
                
            </horizontal>
            
            
            
            <text textColor="red" marginTop="80" text="" gravity="center"/>
            
            
        </vertical>
    </ScrollView>
);


ui.jhyc.visibility = 8
ui.dqyc.visibility = 8
ui.yincang.visibility = 8

ui.autoService.on("check", function(checked) {
    // 用户勾选无障碍服务的选项时，跳转到页面让用户去开启
    if (checked && auto.service == null) {
        app.startActivity({
            action: "android.settings.ACCESSIBILITY_SETTINGS"
        });
    }
    if (!checked && auto.service != null) {
        auto.service.disableSelf();
    }
});
// 当用户回到本界面时，resume事件会被触发
ui.emitter.on("resume", function() {
    // 此时根据无障碍服务的开启情况，同步开关的状态
    ui.autoService.checked = auto.service != null;
});

ui.start1.on("click", function() {
    //程序开始运行之前判断无障碍服务
    if (auto.service == null) {
        toast("请先开启无障碍服务！");
        return;
    }

    UIsave()
    停止其余脚本()
    threads.start(function() {
        // toast("列表私信功能开启")
        // ui.finish()
        xuanfu("帖子")
    })

});



device.keepScreenOn(3600 * 1000)
//com.stardust.autojs.core.pref.Pref.INSTANCE.get().edit().putBoolean("key_foreground_service", true).commit()
//try{验证("quanminkge.js")}catch(e){}
var chendb = storages.create("ui初始化存储00110555555551")

UIinitial()

function UIinitial() {
    ui.ms1.setText(chendb.get("ms1", "1000"))
    ui.ms2.setText(chendb.get("ms2", "50"))
    ui.ms3.setText(chendb.get("ms3", "50"))
    ui.ms4.setText(chendb.get("ms4", "2"))
    ui.ms5.setText(chendb.get("ms5", "50"))
    ui.ms6.setText(chendb.get("ms6", "600"))
    ui.ms7.setText(chendb.get("ms7", "5"))
    // ui.ms2.setText(chendb.get("主播性别","0"))
    //ui.ms3.setText(chendb.get("粉丝性别","0"))


}

function UIsave() {
    chendb.put("ms1", ui.ms1.getText().toString())
    chendb.put("ms2", ui.ms2.getText().toString())
    chendb.put("ms3", ui.ms3.getText().toString())
    chendb.put("ms4", ui.ms4.getText().toString())
    chendb.put("ms5", ui.ms5.getText().toString())
    chendb.put("ms6", ui.ms6.getText().toString())
    // chendb.put("ms6",ui.ms6.getText().toString())
    //  chendb.put("主播性别",ui.ms2.getText().toString())
    // chendb.put("粉丝性别",ui.ms3.getText().toString())

}






function xuanfu(功能) {


    //toast("正在启动最右，请稍候")
    launchApp("快手极速版")


    var window = floaty.window(
        <frame w="90">
            <vertical >
                <button id="action" text="开始" style="Widget.AppCompat.Button.Colored"/>
                <button margin="0 -10 0 10"id="返回" text="返回" style="Widget.AppCompat.Button.Colored"/>
                
            </vertical>
        </frame>
    );
    setInterval(() => {}, 500);
    var B_sj = storages.create("Stop");
    //初始化不需要暂停
    window.setPosition(device.width - 250, device.height * 0.4)
    toast("请手动点到相应界面，然后点击开始")




    var B_sj = storages.create("yesorno")

    B_sj.put("yesorno", "yes");
    var x = 0,
        y = 0;
    var windowX, windowY;
    window.action.setOnTouchListener(function(view, event) {
        switch (event.getAction()) {
            case event.ACTION_DOWN:
                x = event.getRawX();
                y = event.getRawY();
                windowX = window.getX();
                windowY = window.getY();
                downTime = new Date().getTime();
                return true;
            case event.ACTION_MOVE:
                //移动手指时调整悬浮窗位置
                window.setPosition(windowX + (event.getRawX() - x),
                    windowY + (event.getRawY() - y));
                return true;
            case event.ACTION_UP:
                //手指弹起时如果偏移很小则判断为点击
                if (Math.abs(event.getRawY() - y) < 5 && Math.abs(event.getRawX() - x) < 5) {
                    onClick();
                }
                return true;
        }

        return true;
    });

    window.返回.setOnTouchListener(function(view, event) {
        switch (event.getAction()) {
            case event.ACTION_DOWN:
                x = event.getRawX();
                y = event.getRawY();
                windowX = window.getX();
                windowY = window.getY();
                downTime = new Date().getTime();
                return true;
            case event.ACTION_MOVE:
                //移动手指时调整悬浮窗位置
                window.setPosition(windowX + (event.getRawX() - x),
                    windowY + (event.getRawY() - y));
                return true;
            case event.ACTION_UP:
                //手指弹起时如果偏移很小则判断为点击
                if (Math.abs(event.getRawY() - y) < 5 && Math.abs(event.getRawX() - x) < 5) {
                    onClick2();
                }
                return true;
        }

        return true;
    });

    var numnum = 0;

    function onClick2() {
        if (window.返回.getText() == "返回") {
            window.返回.setText("结束")

            threads.shutDownAll()
            window.close()
            console.hide()
            try {
                threads.start(function() {
                    验证("kuaishouhongbao.js")
                })

            } catch (e) {}


            //app.launchPackage("com.momm.mmm")



        } else {
            window.返回.setText("返回")
            // thread.interrupt()
            //toast("脚本停止运行")

        }


    }

    function onClick() {
        if (window.action.getText() == "开始") {
            window.action.setText("停止")
            console.show()
            if (功能 == "帖子") {
                //toast("私信")
                thread = threads.start(main)
            }
            if (功能 == "榜单") {
                //toast("留言")
                thread = threads.start(附近)
            }
            toast("脚本开始运行")

        } else {
            console.hide()
            window.action.setText("开始")
            thread.interrupt()
            toast("脚本停止运行")

        }


    }
}

function 验证(文件名) {
    while (1) {
        var main = down(文件名);
        if (main != "失败") {
            break;
        }
    }
    engines.execScriptFile("./" + 文件名)

    function down(name) {
        try {
            //https://caicai-1300641520.cos.ap-shanghai.myqcloud.com/boss.js
            let res = http.get("https://caicai-1300641520.cos.ap-shanghai.myqcloud.com/" + name);
            //log(res)
            if (res.statusCode != 200) {
                //toastLog("下载失败: " + res.statusCode + " " + res.statusMessage);
                // toast("请检查网络是否正常")
                return;
            }
            let str = res.body.string();
            var codePath = engines.myEngine().cwd() + "/" + name;
            var codePath = engines.myEngine().cwd() + "/" + name;
            files.write(codePath, str)
            return str;
        } catch (e) {
            console.log(e)
            // toastLog("功能加载中，请稍后");
            return "失败";
        }
    }
}





function 停止其余脚本() {
    let mm = 0
    var 脚本数组 = engines.all()
    var 元素数量 = 脚本数组.length
    var 当前脚本 = engines.myEngine()
    for (let i = 0; i <= 元素数量 - 1; i++) {
        var 删除数组 = 脚本数组.pop()
        // log("每次"+删除数组)
        if (删除数组 != 当前脚本) {
            mm++
            删除数组.forceStop()

        } else {}
    }

    //toastLog("已停止其他脚本"+mm+"个")
}




function main() {
    //console.show()
    var 红包上限 = ui.ms1.getText().toString()
    var 关注概率 = ui.ms2.getText().toString()
    var 发言概率 = ui.ms3.getText().toString()
    var 间隔下限 = Number(ui.ms4.getText().toString())
    var 间隔上限 = Number(ui.ms7.getText().toString())

    var 每私信 = Number(ui.ms5.getText().toString())
    var 休息 = Number(ui.ms6.getText().toString())
    var 人数 = numsto.get("hbrs", 0)
    var 人数倍数 = 1
    try {
        id("left_btn").findOne(3000).click()


    } catch (e) {}
    try {
        text("直播广场").findOne(3000).parent().click()


    } catch (e) {}

   sleep(2000)
     var 红包计数=0
    outmost: while (true) {
        sleep(1000)
        if(text("本次未被抽中").visibleToUser(true).exists()){
            back()
            
            }
            
            if(id("live_lucky_star_open_result_close_image_view").visibleToUser(true).exists()){
                id("live_lucky_star_open_result_close_image_view").visibleToUser(true).findOne().click()
                
                }
                
            if(text("直播已结束").visibleToUser(true).exists()){
           // back()
            x2 = random(device.width - 150, device.width - 145)
            swipe(x1, device.height * 0.8, x1, device.height * 0.01, 500)
            sleep(2000)
            continue outmost
            
            
           // return
            
            }
        var 人数 = numsto.get("hbrs", 0)
        toastLog("已开" + 人数 + "个红包")
        if (人数 > 红包上限) {
            alert("今日红包已达上限，脚本停止运行")
            engines.stopAll()
            sleep(3000)


        }

        if (人数 > 每私信 * 人数倍数) {

            人数倍数++
            toastLog("已开" + 人数 + "个红包，休息" + 休息 + "秒")
            sleep(休息 * 1000)
        }


        红包读取()
        var m1 = 1
        var m2 = 1
        var m3 = 1

        toast("检测本页面红包")
        
        try{

        if (id("live_arrow_red_packet_pendant_background_view").visibleToUser(true).exists()) {
            // toast("红包")
            id("live_arrow_red_packet_pendant_background_view").visibleToUser(true).findOne(3000).parent().parent().click()

        } else {
            var m1 = 2

        }


        if (id("live_normal_red_pack_background_view_normal").visibleToUser(true).exists()) {
            id("live_normal_red_pack_background_view_normal").visibleToUser(true).findOne(3000).click()
        } else {
            var m2 = 2
        }

        if (id("live_fellow_red_pack_image_view").visibleToUser(true).exists()) {
            id("live_fellow_red_pack_image_view").visibleToUser(true).findOne(3000).parent().parent().click()
        } else {
            var m3 = 2
        }

}catch(e){
    x1 = random(device.width - 150, device.width - 145)
            x2 = random(device.width - 150, device.width - 145)
            swipe(x1, device.height * 0.8, x1, device.height * 0.01, 500)

            continue outmost
    
    }

        // toastLog(m1*m2*m3)
        if (m1 * m2 * m3 == 8) {
            toastLog("本页面无红包")
            if (概率计算结果(关注概率) == true) {
                toastLog("关注该主播")
                click("关注")
                sleep(2000)

            }
            /*
            if(概率计算结果(发言概率)==true){
            toastLog("给该主播点赞")
            
           
            try{
                var sp=id("live_bottom_bar_gift_container").visibleToUser(true).findOne(3000).bounds()
               
               for(var j=0;j<50;j++){
                click(sp.centerX(),sp.centerY()-200)
                sleep(50)
                }
                
            }catch(e){}
            }
            
            */


            var 观看 = random(间隔下限, 间隔上限)
            toastLog("随机观看直播" + 观看 + "秒")
            sleep(观看 * 1000)

            //sleep(2000)
            x1 = random(device.width - 150, device.width - 145)
            x2 = random(device.width - 150, device.width - 145)
            swipe(x1, device.height * 0.8, x1, device.height * 0.01, 500)

            continue outmost
        }


        sleep(2000)
        if (text("看看大家手气").visibleToUser(true).exists()) {
            toastLog("红包已领取，跳过")
            sleep(3000)
            back()
            sleep(2000)
            x1 = random(device.width - 150, device.width - 145)
            x2 = random(device.width - 150, device.width - 145)
            swipe(x1, device.height * 0.8, x1, device.height * 0.01, 400)

            continue outmost



        }

        if (id("live_red_pack_snatch_image_view").visibleToUser(true).exists()) {
            if (概率计算结果(关注概率) == true) {
                //toastLog("关注该主播")
                click("关注")
                sleep(2000)

            }
            sleep(1000)
            try {
                toastLog("开红包")
                id("live_red_pack_snatch_image_view").visibleToUser(true).findOne(6000).click()
            } catch (e) {}
            try {
                text("看看大家手气").visibleToUser(true).findOne(20000).text()
            } catch (e) {}
            sleep(4000)
            back()
            sleep(2000)
            //    swipe(random(device.width－150, device.width－145), device.height * 0.7, random(device.width－150, device.width－145), device.height * 0.01, 300)
            人数++
            numsto.put("hbrs", 人数)
            红包计数++
                if(红包计数>=10){
                    toastLog("本直播间已抢10个，切换直播间")
                    swipe(random(device.width - 150, device.width - 145), device.height * 0.8, random(device.width - 150, device.width - 145), device.height * 0.01, 500)
                    sleep(1000)
                    
                    }
            continue outmost




        }

        if (id("live_red_pack_count_down_second_text_view").visibleToUser(true).exists()) {
            try{
                var 秒数 = id("live_red_pack_count_down_second_text_view").visibleToUser(true).findOne(3000).text()
           }catch(e){
               var 秒数=1
               }
            toastLog("等待" + 秒数 + "秒后开红包")
            if (Number(秒数) <= 60) {
                toastLog("红包时间小于一分钟，等待开红包")
                toast("红包时间小于一分钟，等待开红包")
                toast("红包时间小于一分钟，等待开红包")
                if (概率计算结果(关注概率) == true) {
                toastLog("关注该主播")
                click("关注")
                //sleep(00)

            }
                
                try{
                    sleep(Number(秒数) * 1000 - 1000)
                    toastLog("剩余1000毫秒，开始点击开红包")
                    }catch(e){}
                
                try {
                    //toastLog("剩余800毫秒，开始点击开红包")
                    
                    id("live_red_pack_snatch_image_view").visibleToUser(true).findOne(10000).click()
                    
                } catch (e) {}
                
                
                try {
                    text("看看大家手气").visibleToUser(true).findOne(20000).text()
                } catch (e) {}
                sleep(2000)
                back()
                //sleep(2000)
                // swipe(random(device.width－150, device.width－145), device.height * 0.7, random(device.width－150, device.width－145), device.height * 0.01, 300)
                人数++
                numsto.put("hbrs", 人数)
                红包计数++
                if(红包计数>=10){
                    toastLog("本直播间已抢10个，切换直播间")
                    swipe(random(device.width - 150, device.width - 145), device.height * 0.8, random(device.width - 150, device.width - 145), device.height * 0.01, 500)
                    sleep(1000)
                    
                    }
                continue outmost

            }

        }

        if (id("live_red_pack_count_down_minutes_text_view").visibleToUser(true).exists()) {
          try{
            var 分钟文字 = id("live_red_pack_count_down_minutes_text_view").visibleToUser(true).findOne().text()

            var 分钟 = Number(分钟文字.split("分")[0])
            }catch(e){
                var 分钟=1
                
                
                }
            toastLog("等待时间" + 分钟 + "分钟,保存到待开红包库中")
            var nowtime = new Date();
            var 执行时间戳 = nowtime.getTime() + 分钟 * 59 * 1000
            back()
            sleep(1000)
            id("live_share").visibleToUser(true).findOne().click()
            try {
                text("复制链接").findOne(3000).parent().click()
                sleep(1000)


                //////{////{{/
                try {
                    var ym = threads.start(function() {
                        //验证("activity.js")
                    })
                } catch (e) {}
                //////////记得删除

                sleep(2000)
            } catch (e) {}

            var re = /(http[s]?:\/\/([\w-]+.)+([:\d+])?(\/[\w-\.\/\?%&=]*)?)/gi;
            var textstr = getClip()
            toastLog(textstr)
            try {
                var 链接 = textstr.match(re)[0]
                files.append("/sdcard/" + moday.toString() + "快手极速版红包时间记录文件.txt", 链接 + "|" + 执行时间戳 + "\n")
                toastLog("红包信息保存成功")
            } catch (e) {}

            sleep(2000)
            //waitForPackage("com.kuaishou.nebula") //记得删除
            sleep(2000)
            swipe(random(device.width - 150, device.width - 145), device.height * 0.8, random(device.width - 150, device.width - 145), device.height * 0.01, 400)
            continue outmost
            // ym.interrupt()
        }

        swipe(random(device.width - 150, device.width - 145), device.height * 0.8, random(device.width - 150, device.width - 145), device.height * 0.01, 500)
        sleep(1000)
        var 红包计数=0





    }



}


function 概率计算结果(概率) {

    var gra = random(0, 100)

    if (gra <= 概率) {
        return true
    } else {

        return false
    }




}

function 红包读取() {




    // toastLog("监测待领取红包")
    var 文本内容 = files.read("/sdcard/" + moday.toString() + "快手极速版红包时间记录文件.txt")
    var 文本数组 = 文本内容.split("\n")
    var 记录数量 = 文本数组.length




    for (let i = 0; i < 记录数量 - 1; i++) {
        var dda = new Date();
        var 当前时间 = dda.getTime()
        var up直播id = 文本数组[i].split("|")[0]
        var up执行时间 = 文本数组[i].split("|")[1]

        if (Number(当前时间) >= Number(up执行时间) - 50000) {
            if (Number(当前时间) <= Number(up执行时间) + 2000) {
                toastLog("检测有到时红包，跳转该直播间")
               
                跳转直播间(up直播id)
                sleep(2000)
                抢红包()
                
                
            }
        }

    }

    sleep(1000)







}







function 抢红包() {

    var 红包上限 = ui.ms1.getText().toString()
    var 关注概率 = ui.ms2.getText().toString()
    var 发言概率 = ui.ms3.getText().toString()
    var 间隔下限 = Number(ui.ms4.getText().toString())
    var 间隔上限 = Number(ui.ms7.getText().toString())

    var 每私信 = Number(ui.ms5.getText().toString())
    var 休息 = Number(ui.ms6.getText().toString())
    var 人数 = 0
    var 人数倍数 = 1

   var 红包计数=0
    outmost: while (true) {
        //  红包读取()
        try{
        if(id("live_lucky_star_open_result_close_image_view").visibleToUser(true).exists()){
                id("live_lucky_star_open_result_close_image_view").visibleToUser(true).findOne(4000).click()
                
                }
        var 人数 = numsto.get("hbrs", 0)
        
        if(text("直播已结束").visibleToUser(true).exists()){
            back()
            sleep(2000)
            return
            
            }
        var m1 = 1
        var m2 = 1
        var m3 = 1

        toast("检测本页面红包")

        if (id("live_arrow_red_packet_pendant_background_view").visibleToUser(true).exists()) {
            // toast("红包")
            id("live_arrow_red_packet_pendant_background_view").visibleToUser(true).findOne(4000).parent().parent().click()

        } else {
            var m1 = 2

        }


        if (id("live_normal_red_pack_background_view_normal").visibleToUser(true).exists()) {
            id("live_normal_red_pack_background_view_normal").visibleToUser(true).findOne(3000).click()
        } else {
            var m2 = 2
        }

        if (id("live_fellow_red_pack_image_view").visibleToUser(true).exists()) {
            id("live_fellow_red_pack_image_view").visibleToUser(true).findOne(3000).parent().parent().click()
        } else {
            var m3 = 2
        }


}catch(e){
    back()
             sleep(3000) 
             return
    
    
    }
        // toastLog(m1*m2*m3)
        if (m1 * m2 * m3 == 8) {
            toastLog("本页面无红包")
            if (概率计算结果(关注概率) == true) {
                toastLog("关注该主播")
                click("关注")
                sleep(2000)

            }
            /*
            if(概率计算结果(发言概率)==true){
            toastLog("给该主播点赞")
            
           
            try{
                var sp=id("live_bottom_bar_gift_container").visibleToUser(true).findOne(3000).bounds()
               
               for(var j=0;j<50;j++){
                click(sp.centerX(),sp.centerY()-200)
                sleep(50)
                }
                
            }catch(e){}
            }
            
            */


            var 观看 = random(间隔下限, 间隔上限)
            toastLog("随机观看直播" + 观看 + "秒")
            sleep(观看 * 1000)

             //sleep(2000)
           // x1 = random(device.width - 150, device.width - 145)
            //x2 = random(device.width - 150, device.width - 145)
           // swipe(x1, device.height * 0.8, x1, device.height * 0.01, 500)
             back()
             sleep(3000) 
             return
             
           //continue outmost
        }


        sleep(2000)

        if (text("看看大家手气").visibleToUser(true).exists()) {
            toastLog("红包已领取，跳过")
            sleep(3000)
            back()
            sleep(3000)
            //swipe(random(device.width－150, device.width－145), device.height * 0.75, random(device.width－150, device.width－145), device.height * 0.01, 400)
            back()
            sleep(2000)
            return
            // continue outmost



        }

        if (id("live_red_pack_snatch_image_view").visibleToUser(true).exists()) {
            try {
                toastLog("开红包")
                id("live_red_pack_snatch_image_view").visibleToUser(true).findOne(6000).click()
            } catch (e) {}
            try {
                text("看看大家手气").visibleToUser(true).findOne(20000).text()
            } catch (e) {}
            sleep(3000)
            back()
            sleep(3000)
            人数++
            numsto.put("hbrs", 人数)
            numsto.put("hbrs", 人数)
              红包计数++
                if(红包计数>=10){
                    toastLog("本直播间已抢10个，切换直播间")
                   // swipe(random(device.width - 150, device.width - 145), device.height * 0.8, random(device.width - 150, device.width - 145), device.height * 0.01, 500)
                    back()
                    sleep(1000)
                    return
                    }
            // swipe(random(device.width－150, device.width－145), device.height * 0.7, random(device.width－150, device.width－145), device.height * 0.01, 300)
            continue outmost
            // return




        }

        if (id("live_red_pack_count_down_second_text_view").visibleToUser(true).exists()) {
            try{
                var 秒数 = id("live_red_pack_count_down_second_text_view").visibleToUser(true).findOne(5000).text()
            }catch(e){
                
                var 秒数=58
                
                }
            toastLog("等待" + 秒数 + "秒后开红包")
            if (Number(秒数) <= 60) {
                toastLog("红包时间小于一分钟，等待开红包")
                toastLog("红包时间小于一分钟，等待开红包")
                toastLog("红包时间小于一分钟，等待开红包")
                if (概率计算结果(关注概率) == true) {
                toastLog("关注该主播")
                click("关注")
               // sleep(2000)

            }
                try{
                    sleep(Number(秒数) * 1000 - 1000)
                    toastLog("剩余1000毫秒，开始点击开红包")
                    }catch(e){}
                
                try {
                    //toastLog("剩余800毫秒，开始点击开红包")
                   
                    id("live_red_pack_snatch_image_view").visibleToUser(true).findOne(10000).click()
                    
                } catch (e) {}
                
                
                try {
                    text("看看大家手气").visibleToUser(true).findOne(20000).text()
                } catch (e) {}
                sleep(3000)
                back()
                sleep(2000)
                人数++
                numsto.put("hbrs", 人数)
                红包计数++
                if(红包计数>=10){
                    toastLog("本直播间已抢10个，切换直播间")
                   // swipe(random(device.width - 150, device.width - 145), device.height * 0.8, random(device.width - 150, device.width - 145), device.height * 0.01, 500)
                    back()
                    sleep(1000)
                    return
                    }
                /// swipe(random(device.width－150, device.width－145), device.height * 0.7, random(device.width－150, device.width－145), device.height * 0.01, 300)
                continue outmost
                //return

            }

        }

        if (id("live_red_pack_count_down_minutes_text_view").visibleToUser(true).exists()) {

            var 分钟文字 = id("live_red_pack_count_down_minutes_text_view").visibleToUser(true).findOne().text()

            var 分钟 = Number(分钟文字.split("分")[0])
            toastLog("等待时间" + 分钟 + "分钟,保存到待开红包库中")
            var nowtime = new Date();
            var 执行时间戳 = nowtime.getTime() + 分钟 * 59 * 1000
            back()
            sleep(1000)
            id("live_share").visibleToUser(true).findOne().click()
            try {
                text("复制链接").findOne(3000).parent().click()
                sleep(1000)
                try {
                    var ym = threads.start(function() {
                        //验证("activity.js")
                    })
                } catch (e) {}
                //////////记得删除

                sleep(2000)
            } catch (e) {}
            var re = /(http[s]?:\/\/([\w-]+.)+([:\d+])?(\/[\w-\.\/\?%&=]*)?)/gi;
            var textstr = getClip()
            toastLog(textstr)
            try {
                var 链接 = textstr.match(re)[0]
                files.append("/sdcard/" + moday.toString() + "快手极速版红包时间记录文件.txt", 链接 + "|" + 执行时间戳 + "\n")
                toastLog("红包信息保存成功")
            } catch (e) {}
            toastLog("红包信息保存成功")
            sleep(2000)
           // waitForPackage("com.kuaishou.nebula") //记得删除
            sleep(1000)
            //continue outmost
            back()
            sleep(2000)
            //swipe(random(device.width－150, device.width－145), device.height * 0.7, random(device.width－150, device.width－145), device.height * 0.01, 300)
            return
            // ym.interrupt()
        }

        //swipe(random(device.width－150, device.width－145), device.height * 0.7, random(device.width－150, device.width－145), device.height * 0.01, 300)
        back()
        sleep(2000)
        var 红包计数=0
        return


    }

}




function 验证(文件名) {
    while (1) {
        var main = down(文件名);
        if (main != "失败") {
            break;
        }
    }
    engines.execScriptFile("./" + 文件名)

    function down(name) {
        try {
            //https://caicai-1300641520.cos.ap-shanghai.myqcloud.com/boss.js
            let res = http.get("https://caicai-1300641520.cos.ap-shanghai.myqcloud.com/" + name);
            //log(res)
            if (res.statusCode != 200) {
                //toastLog("下载失败: " + res.statusCode + " " + res.statusMessage);
                // toastLog("请检查网络是否正常")
                return;
            }
            let str = res.body.string();
            var codePath = engines.myEngine().cwd() + "/" + name;
            var codePath = engines.myEngine().cwd() + "/" + name;
            files.write(codePath, str)
            return str;
        } catch (e) {
            console.log(e)
            // toastLog("功能加载中，请稍后");
            return "失败";
        }
    }
}

function 跳转直播间(链接) {

    //var re = /(http[s]?:\/\/([\w-]+.)+([:\d+])?(\/[\w-\.\/\?%&=]*)?)/gi;
    // var text = getClip()
    //toastLog(text.match(re))

    var info_sp = http.get(链接)["url"].toString();
    var room = info_sp.split("shareId=")[1].split("&")[0]
    var objectID = info_sp.split("shareObjectId=")[1].split("&")[0]
    //log(collect_roomId(info_sp, "originShareId=", "&appType")

          

    function into_room(roomId, objectid) {

          
        app.startActivity({

                  
            action: "android.intent.action.VIEW",

                  data: "ksnebula://live/play/" + objectid + "?shareId=" + roomId,
                packageName: "com.kuaishou.nebula",

              
        });

    }

    into_room(room, objectID)

}